﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using TIS.DAO;

namespace TIS.Administrator
{
    public partial class ManagerCat : System.Web.UI.Page
    {
        CategoryDAO _categoryDao = new CategoryDAO();
        NewsDAO _newsDao = new NewsDAO();
        LanguageDAO _languageDao = new LanguageDAO();
        UserInfo _userInfo = new UserInfo();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["langId"] == null)
                {
                    Session["langId"] = 1;
                }

                if (Session["user"] != null)
                {

                    GetDataDdlLanguage();
                    BindCategory();
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }


        public void GetDataDdlLanguage()
        {

            ddlLanguage.DataSource = _languageDao.GetAllLanguage();
            ddlLanguage.DataTextField = "LanguageName";
            ddlLanguage.DataValueField = "LanguageId";
            ddlLanguage.SelectedValue = Session["langId"].ToString();
            ddlLanguage.DataBind();
        }

        public void BindCategory()
        {

            grvCategory.DataSource = _categoryDao.ListCategory(int.Parse(Session["langId"].ToString()));
            grvCategory.DataBind();

        }

        protected void btnLoadGrid_Click(object sender, EventArgs e)
        {
            Session["langId"] = ddlLanguage.SelectedValue;
            DataTable dt = _categoryDao.ListCategory(int.Parse(Session["langId"].ToString()));
            if (dt.Rows.Count > 0)
            {
                grvCategory.DataSource = dt;
                grvCategory.DataBind();
            }
        }

        protected void lbtnRemove_Command(object sender, CommandEventArgs e)
        {
            Session["langId"] = ddlLanguage.SelectedValue;
            DataTable dt =
                _newsDao.GetNewsByCatId(int.Parse(e.CommandArgument.ToString()), int.Parse(Session["langId"].ToString()));
            if (dt.Rows.Count > 0)
            {
                ltlMessage.Text = @"<div class='alert alert-error'>
                                                <h4>Error!</h4>
                                                Còn bản ghi trong danh mục không thể xóa...
                                              </div>";
            }
            else
            {
                _categoryDao.DeleteCategory(int.Parse(e.CommandArgument.ToString()));
                ltlMessage.Text = @"<div class='alert alert-success'>
                                                        <h4>
                                                            Success!</h4>
                                                        Xóa thành công...
                                                    </div>";
                BindCategory();
            }
        }

        protected void lbtnEdit_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect("Home.aspx?id=" + e.CommandArgument.ToString());
        }

        protected void grvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvCategory.PageIndex = e.NewPageIndex;
            BindCategory();
        }
    }
}